package leetcode

//https://leetcode.com/problems/longest-common-prefix/


fun main(args: Array<String>) {
    println(LongestCommonPrefix().longestCommonPrefix(arrayOf("flower","flow","flight")))
    println(LongestCommonPrefix().longestCommonPrefix(arrayOf("dog","racecar","car")))
}
class LongestCommonPrefix {

    fun longestCommonPrefix(strs: Array<String>): String {
        if (strs.isEmpty())return ""
        var result = ""
        var smallestLen = Int.MAX_VALUE
        var i = 0
        while (i < strs.size) {
            val length = strs[i].length
            if (length < smallestLen)
                smallestLen = length
            i++
        }

        var j = 0
        while (j < smallestLen) {
            var k = 0
            while (k < strs.size - 1) {
                if (strs[k][j] != strs[k + 1][j])
                    return result
                k++
            }
            result += strs[0][j]
            j++
        }
        return result
    }
}